//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Globalization;
using System.IO;
using System.Configuration;

using DNAide.Core;
using DNAide.Configuration;

namespace DNAide.Web
{

	/// <summary>
	/// Module - including will move ViewState to the bottom of your webform
	/// </summary>
	public class ViewStateModule : IHttpModule
	{

		#region IHttpModule Members

		/// <summary>
		/// </summary>
		public void Dispose()
		{
		}

		/// <summary>
		/// </summary>
		/// <param name="context"></param>
		public void Init(HttpApplication context)
		{
			if (context == null)
			{
				// Do nothing
				return;
			}

			if (DNAideConfigurationSettings.Web.ViewStateModule.FiresOnEvent == HttpModuleFiringEvent.None)
			{
				// Do nothing
				return;
			}

			// Set the event handler
			switch (DNAideConfigurationSettings.Web.ViewStateModule.FiresOnEvent)
			{
				case HttpModuleFiringEvent.BeginRequest:
					context.BeginRequest += new EventHandler(Application_BeginRequest);
					break;
				case HttpModuleFiringEvent.AuthenticateRequest:
					context.AuthenticateRequest += new EventHandler(Application_AuthenticateRequest);
					break;
				case HttpModuleFiringEvent.AuthorizeRequest:
					context.AuthorizeRequest += new EventHandler(Application_AuthorizeRequest);
					break;
			}
		}

		#endregion


		private void HandleRequest(HttpApplication app)
		{
			string fileExtension = Path.GetExtension(app.Request.Path).ToString();

			if (!CheckApplicableFileExtension(fileExtension))
			{
				// Not an applicable file extension so do nothing
				return;
			}

			app.Response.Filter = new ViewStateFilter(app.Response.Filter);
		}


		private static bool CheckApplicableFileExtension(string fileExtension)
		{
			bool retval = false;
			if (!String.IsNullOrEmpty(DNAideConfigurationSettings.Web.ViewStateModule.ApplicableFileExtensions))
			{
				// If it's and invalid file extension for the module, then do nothing
				if (String.CompareOrdinal(DNAideConfigurationSettings.Web.ViewStateModule.ApplicableFileExtensions, ".*") == 0)
				{
					retval = true;
				}
				else
				{
					string[] applicableFileExtensions = DNAideConfigurationSettings.Web.ViewStateModule.ApplicableFileExtensions.Split(new char[] { ',' });
					foreach (string applicableFileExtension in applicableFileExtensions)
					{
						if (String.CompareOrdinal(applicableFileExtension.Trim().ToLower(), fileExtension.ToLower()) == 0)
						{
							retval = true;
							break;
						}
					}
				}
			}
			return retval;
		}


		#region Event Handlers

		private void Application_AuthorizeRequest(object sender, EventArgs e)
		{
			HttpApplication app = sender as HttpApplication;
			HandleRequest(app);
		}


		private void Application_AuthenticateRequest(object sender, EventArgs e)
		{
			HttpApplication app = sender as HttpApplication;
			HandleRequest(app);
		}


		private void Application_BeginRequest(object sender, EventArgs e)
		{
			HttpApplication app = sender as HttpApplication;
			HandleRequest(app);
		}

		#endregion

	}
}
